package com.toraeve.asktora.domain.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.toraeve.asktora.domain.entity.ToraUser;
import com.toraeve.asktora.domain.repository.UserRepository;

@Service
public class UserService {

	private static Log logger = LogFactory.getLog(UserService.class);
	private UserRepository userRepository;

	@Required
	@Autowired
	public void setUserRepository(UserRepository userRepository) {
		Assert.notNull(userRepository, "userRepository cannot be null!");
		this.userRepository = userRepository;
	}

	public ToraUser getUserByEmail(String userEmail) {
		Assert.notNull(userEmail, "argument:[userEmail] cannot be null!");
		logger.debug("Tring to get user with email:" + userEmail);

		ToraUser user = userRepository.getUserByEmail(userEmail);

		logger.debug("Returning user:" + user);
		return user;
	}

	@Transactional
	public void addUser(ToraUser newUser) {
		Assert.notNull(newUser, "argument:[newUser] cannot be null!");
		logger.debug("Tring to add new user:" + newUser);

		userRepository.addUser(newUser);

		logger.debug("New User:" + newUser + " added successfully!");
	}

	@Transactional
	public void updateUser(ToraUser user) {
		Assert.notNull(user, "argument:[user] cannot be null!");
		logger.debug("Tring to update user:" + user);

		userRepository.updateUser(user);

		logger.debug("User:" + user + " updated successfully!");
	}

	@Transactional
	public void deleteUser(String userEmail) {
		Assert.notNull(userEmail, "argument:[userEmail] cannot be null!");
		logger.debug("Tring to delete user with email:" + userEmail);

		userRepository.deleteUser(userEmail);

		logger.debug("User with Email:" + userEmail
				+ " marked deleted successfully!");
	}

}
